home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C
/
Applications
/
MacGzip 1.0
/
source
/
Mac
/
Prefs.c
< prev
next >
Wrap
Text File
|
1995-08-18
|
7KB
|
273 lines
/*
* Prefs.c
*
* SPDsoft, August 14, 1995
* Based on Greg Robbins' "Sample System 7 Preferences" (Apple DTS)
*/
#ifndef _FOLDERS_
# include <Folders.h>
#endif
#include "Prefs.h"
/* prefs file constants */
#define kPrefsNameStrID 130 /* name of prefs file */
#define kPrefsCreatorType '\?\?\?\?'
#define kPrefsFileType 'pref'
#define kPrefsResourceType 'Pref'
#define kPrefsResourceID 128
#define kFinderMessageStrID -16397
#define kStrType 'STR '
/*
* Prototypes
*/
static OSErr GetPrefsName (StringPtr prefName, short StrID);
/*
* fill in the prefs structure from the resource file or from scratch
*/
Boolean GetOrGeneratePrefs(PrefsType * thePrefsTypePtr, AliasHandle * fileAliasHandlePtr, long wantedVers )
{
short prefsResRefNum;
Handle tempHandle;
extern PrefsType DefaultPrefs;
/*
* set default prefs
*/
BlockMove( &DefaultPrefs, thePrefsTypePtr, sizeof(PrefsType));
*fileAliasHandlePtr = nil;
/* open (but don't create) the prefs file */
if ( -1 != (prefsResRefNum = OpenPrefsResFile(fsRdPerm, false, kPrefsNameStrID)))
{
/* file opened successfully, get the prefs resource */
tempHandle = Get1Resource(kPrefsResourceType, kPrefsResourceID);
/*
* Assertion: left to right evaluation
*/
if ( (tempHandle != nil) &&
(GetHandleSize(tempHandle) == sizeof(PrefsType)) &&
((*(PrefsType *)*tempHandle).prefsTypeVers == wantedVers)
)
{
/*
* copy the prefs struct and close file
*/
*thePrefsTypePtr = *(PrefsType *)*tempHandle;
/* if there's also an alias, get and detach that, too */
if (thePrefsTypePtr->Folder.UseDestFolder)
{
/* don't orphan a previous alias handle */
if (*fileAliasHandlePtr != nil)
DisposHandle((Handle) *fileAliasHandlePtr);
*fileAliasHandlePtr = (AliasHandle)
Get1Resource(rAliasType, thePrefsTypePtr->Folder.folderAliasID);
if (*fileAliasHandlePtr != nil)
DetachResource((Handle) *fileAliasHandlePtr);
}
CloseResFile(prefsResRefNum);
}
else
{
/*
* Current prefs can no longer be used
* because they are incompatible with current prefs version
* or otherwise malformed.
*/
CloseResFile(prefsResRefNum);
return false;
}
return true;
}
else
return false;
}
OSErr SavePrefs(PrefsType * thePrefsTypePtr, AliasHandle * fileAliasHandlePtr )
{
OSErr retCode;
short prefsResRefNum;
Handle prefHandle,
alisHandle,
finderMessageHandle;
/* open (and, if necessary, create) the prefs file */
prefsResRefNum = OpenPrefsResFile(fsRdWrPerm, true, kPrefsNameStrID);
if (prefsResRefNum != -1)
{
/* file opened successfully, get the prefs resource */
prefHandle = Get1Resource(kPrefsResourceType, kPrefsResourceID);
if (prefHandle == nil)
{
/* create a new resource */
prefHandle = NewHandle(sizeof(PrefsType));
if (prefHandle != nil)
{
/* copy the prefs struct into the handle
and make it into a resource */
*(PrefsType *)*prefHandle = *thePrefsTypePtr;
AddResource(prefHandle, kPrefsResourceType, kPrefsResourceID, "\pPrefsType");
retCode = ResError();
if (retCode != noErr)
DisposHandle(prefHandle);
}
else
retCode = MemError(); /* NewHandle failed */
}
else
{ /* prefHandle != nil */
/* update the existing resource */
SetHandleSize(prefHandle, sizeof(PrefsType));
retCode = MemError();
if (retCode == noErr)
{
/* copy the prefs struct into the handle and tell the rsrc manager */
*(PrefsType *)*prefHandle = *thePrefsTypePtr;
ChangedResource(prefHandle);
}
}
if (retCode == noErr)
{
/* now, get rid of the old fileAlias and, if the fileAliasID field
of the prefs struct indicates that there is a new one, add it to
the resource file */
/* remove any old file alias */
alisHandle = Get1Resource(rAliasType, thePrefsTypePtr->Folder.folderAliasID);
if (alisHandle != nil) {
RmveResource(alisHandle);
DisposHandle(alisHandle);
}
/* if appropriate, add the current alias to the resource file */
if ( thePrefsTypePtr->Folder.UseDestFolder && *fileAliasHandlePtr != nil)
{
AddResource((Handle) *fileAliasHandlePtr, rAliasType,
thePrefsTypePtr->Folder.folderAliasID, "\pfolder alias");
/* keep the alias available */
DetachResource((Handle) *fileAliasHandlePtr);
}
/* add the message to be displayed if the user tries
to open the prefs file in the Finder (but don't add it
if it's already in the preferences file) */
finderMessageHandle = (Handle) GetString(kFinderMessageStrID);
if (finderMessageHandle != nil &&
HomeResFile((Handle) finderMessageHandle) != prefsResRefNum)
{
/* copy the resource into the prefs file */
DetachResource(finderMessageHandle);
AddResource(finderMessageHandle, kStrType, kFinderMessageStrID,
"\pFinder message");
/* if AddResource failed, dispose of the handle */
retCode = ResError();
if (retCode != noErr) DisposHandle(finderMessageHandle);
}
}
/* update and close the preference resource file,
releasing its resources from memory */
CloseResFile(prefsResRefNum);
}
else
{
/* couldn't open the res file */
retCode = ResError();
if (retCode == noErr) retCode = resFNotFound;
}
return retCode;
}
/******************************************************************************/
short OpenPrefsResFile(SignedByte prefsPerm, Boolean createFlag, short StrID)
/* open the preferences file with the given permission; if createFlag is set,
create a preferences file if necessary */
{
OSErr retCode;
short prefsVRefNum;
long prefsDirID;
Str255 prefsNameStr;
FSSpec prefsFSSpec;
short prefsResRefNum;
prefsResRefNum = -1;
if (noErr == (retCode = GetPrefsName(prefsNameStr, StrID)) )
{
retCode = FindFolder( kOnSystemDisk,
kPreferencesFolderType,
kCreateFolder,
&prefsVRefNum,
&prefsDirID);
if (retCode == noErr) {
/* make a file spec for the prefs file */
retCode = FSMakeFSSpec(prefsVRefNum, prefsDirID, prefsNameStr, &prefsFSSpec);
if (retCode == fnfErr && createFlag)
{
/* prefs file doesn't already exist, so create it */
FSpCreateResFile(&prefsFSSpec, kPrefsCreatorType, kPrefsFileType, smSystemScript);
retCode = ResError();
}
/* open the prefs file */
if (retCode == noErr)
{
prefsResRefNum = FSpOpenResFile(&prefsFSSpec, prefsPerm);
}
}
}
return prefsResRefNum;
}
static OSErr GetPrefsName (StringPtr prefName, short StrID)
/* get the name of the preferences file from a resource in the application */
{
StringHandle prefStrHandle;
OSErr retCode = noErr;
if ( nil != (prefStrHandle = GetString(StrID)))
BlockMove(*prefStrHandle, prefName, (Size) (**prefStrHandle) + 1);
else
retCode = resNotFound;
return retCode;
}